package com.cskaoyan.market.controller.admin;


import com.cskaoyan.market.annotation.RequiresPermissionsDesc;
import com.cskaoyan.market.db.domain.MarketTopic;
import com.cskaoyan.market.service.admin.MarketTopicService;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RequiresAuthentication
@RestController
@RequestMapping("/admin/topic")
@Validated
public class AdminTopicController{

    private final Log logger = LogFactory.getLog(AdminTopicController.class);

    @Autowired
    MarketTopicService marketTopicService;

    @RequiresPermissions("admin:topic:list")
    @RequiresPermissionsDesc(menu = {"推广管理", "专题管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(Integer page, Integer limit, String title, String subtitle, String sort, String order) {

        // sort若为空，则取默认值add_time
        if (StringUtils.isEmpty(sort)) {
            sort = "add_time";
        }
        // order若为空，则取默认值desc - 降序
        if (StringUtils.isEmpty(order)) {
            order = "desc";
        }
        List<MarketTopic> result = marketTopicService.list(page, limit, sort, order,title,subtitle);

        return ResponseUtil.okList(result);
    }

    @RequiresPermissions("admin:topic:create")
    @RequiresPermissionsDesc(menu = {"推广管理", "专题管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody MarketTopic marketTopic) {

        // 添加参数
        LocalDateTime now = LocalDateTime.now();
        marketTopic.setAddTime(now);
        marketTopic.setUpdateTime(now);
        marketTopic.setDeleted(false);
        //根据size是否是1还是0判断广告管理信息是否上传成功
        int resCode = marketTopicService.create(marketTopic);
        if (resCode == 0) {
            // 上传失败
            return ResponseUtil.badArgument();
        }
        return ResponseUtil.ok(marketTopic);
    }

    @RequiresPermissions("admin:topic:update")
    @RequiresPermissionsDesc(menu = {"推广管理", "专题管理"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody MarketTopic marketTopic) {

        //根据resCode是否是1还是0判断广告管理信息是否更新成功
        int resCode = marketTopicService.update(marketTopic);
        if (resCode == 0) {
            // 更新失败
            return ResponseUtil.updatedDataFailed();
        }
        // 更新成功
        // 设置响应体
        return ResponseUtil.ok(marketTopic);
    }

    @RequiresPermissions("admin:topic:delete")
    @RequiresPermissionsDesc(menu = {"推广管理", "专题管理"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody MarketTopic marketTopic) throws IOException {

        int resCode = marketTopicService.delete(marketTopic);
        if (resCode == 0) {
            return ResponseUtil.fail();
        }
        return ResponseUtil.ok(marketTopic);
    }

    @RequiresPermissions("admin:topic:read")
    @RequiresPermissionsDesc(menu = {"推广管理", "专题管理"}, button = "详情")
    @GetMapping("/read")
    public Object read(Integer id) {

        Map<String , Object> result = marketTopicService.getRead(id);

        return ResponseUtil.ok(result);
    }
}
